"""团购活动页面"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from page.login_page import LoginPage
from selenium import webdriver
from time import sleep
class GroupBuy():
    loc_cuxi = (By.CSS_SELECTOR, "#menu-ul > li.collapse.lis.ico_2")
    loc_clickgroup = (By.XPATH, "/html/body/div[2]/div[1]/ul/li[2]/ul/li[5]/a")
    loc_addgroup = (By.XPATH, "/html/body/h1/span[1]/a")
    loc_search_a = (By.XPATH, '/html/body/div[1]/form/input[2]')
    loc_selecta = (By.CSS_SELECTOR, "#group-table > tbody > tr:nth-child(1) > td:nth-child(2) > select")
    loc_input_price = (By.CSS_SELECTOR, "#group-table > tbody > tr:nth-child(7) > td:nth-child(2) > input[type=text]:nth-child(1)")
    loc_input_price_a = (By.CSS_SELECTOR, '#group-table > tbody > tr:nth-child(7) > td:nth-child(2) > input[type=text]:nth-child(2)')
    loc_click_btn = (By.XPATH, '/html/body/form/div/table/tbody/tr[9]/td[2]/input[2]')
    loc_input_keys = (By.XPATH, "//*[@id='keyword']")
    loc_search_b= (By.XPATH, "/html/body/div[2]/div[2]/div/form/span[2]")
    loc_photo = (By.XPATH, "//*[@id='compareForm']/div/div/a/img")
    loc_ecs_formbuy=(By.XPATH,"//*[@id='ECS_FORMBUY']/ul[3]/li[2]/a/img")
    loc_center_buy = (
            By.CSS_SELECTOR, "body > div.block.table > div.flowBox > table > tbody > tr > td:nth-child(2) > a > img")
    loc_sunmit_buy = (
            By.CSS_SELECTOR, "#theForm > div:nth-child(14) > div:nth-child(3) > input[type=image]:nth-child(1)")
    loc_username = (By.XPATH, "//*[@id='username']")
    loc_password = (By.XPATH, "//*[@id='loginForm']/table/tbody/tr[2]/td[2]/input")
    loc_click_btn_a = (By.XPATH, "//*[@id='loginForm']/table/tbody/tr[5]/td/div/input[1]")
    loc_use_red_package = (By.CSS_SELECTOR, "#ECS_BONUS")
    def __init__(self, driver):
        self.driver = driver
    def cuxi(self):
    # 操作方法
    # 点击促销
        self.driver.find_element(*self.loc_cuxi).click()
    def click_group(self):
        # 点击添加团购
        self.driver.find_element(*self.loc_clickgroup).click()
        #退出菜单的frame
        self.driver.switch_to.parent_frame()
        # 切入主界面的frame
        self.driver.switch_to.frame("main-frame")
    def add_group(self):
        #添加活动
        self.driver.find_element(*self.loc_addgroup).click()
    def search(self):
        #点击搜索
        self.driver.find_element(*self.loc_search_a).click()
    def selecta(self):
        #选择商品
        self.driver.find_element(*self.loc_selecta).send_keys("现货发售 马海毛保暖毛衣")
    def input_price(self):
        self.driver.find_element(*self.loc_input_price).send_keys("1")
        sleep(4)
        self.driver.find_element(*self.loc_input_price_a).send_keys("50")
        sleep(4)
    def click_btn(self):
        #点击确定
        self.driver.find_element(*self.loc_click_btn).click()
        sleep(3)
        #验证活动是否添加成功
    def get_page(self):
        self.driver.get("http://192.168.4.54:8081/upload/")
    #输入搜索内容
    def input_keys(self):
        self.driver.find_element(*self.loc_input_keys).send_keys("现货发售 马海毛保暖毛衣")
    #点击搜索
    def search_btn(self):
        self.driver.find_element(*self.loc_search_b).click()
    #点击搜索的商品
    def photo(self):
        self.driver.find_element(*self.loc_photo).click()
    ##点击立即购买
    def ecs_formbuy(self):
        self.driver.find_element(*self.loc_ecs_formbuy).click()
    #点击购物中心
    def center_buy(self):
        self.driver.find_element(*self.loc_center_buy).click()
    sleep(4)
    #登录
    def lg_user(self):
        self.driver.find_element(*self.loc_username).send_keys("lwx")
        self.driver.find_element(*self.loc_password).send_keys("123456")
        self.driver.find_element(*self.loc_click_btn_a).click()
    def use_red_package(self):
        self.driver.find_element(*self.loc_use_red_package).send_keys("红包[￥222.00元]")
    def submit_buy(self):
        self.driver.find_element(*self.loc_sunmit_buy).click()
        sleep(5)
    #汇总
    def addgroupbuy(self):
        self.cuxi()
        self.click_group()
        self.add_group()
        self.search()
        self.selecta()
        self.click_btn()
        self.input_price()
        self.get_page()
        self.input_keys()
        self.search_btn()
        self.photo()
        sleep(3)
        self.ecs_formbuy()
        sleep(3)
        self.center_buy()
        sleep(3)
        self.lg_user()
        sleep(5)
        self.use_red_package()
        sleep(5)
        self.submit_buy()
        sleep(3)